﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZSPG_ZSPersonInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        Button Button1;
        Button btnInsert, btnUpload;
        TextBox 项目名称;
        CheckBox ckb是否已故;
        DropDownList txt信息完整;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            Antu.WebControls.MPHeader.RegistAjaxScripts(typeof(ZSPG_ZSPersonInfoControl));
            Button1 = (Button)FindControl("Button1");
            btnInsert = (Button)FindControl("btnInsert");
            项目名称 = (TextBox)FindControl("项目名称");
            ckb是否已故 = (CheckBox)FindControl("ckb是否已故");
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_ZSPERSON";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["id"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
                QSDataDLL();
                BindDll();
            }
            Persistence1.BindControl = panel1;
            BindData();
            Button1.Click += new EventHandler(Button1_Click);
            btnInsert.Click+=new EventHandler(btnInsert_Click);
            if (Page.Request["id"]!=null)
            {
                btnInsert.Visible = false;
            }
            ckb是否已故.CheckedChanged+=new EventHandler(ckb是否已故_CheckedChanged);
            IsLookPerson();
            IsShiPerson();
        }

        private void IsShiPerson()
        {
            string sql = "Select * from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + @"') And(Rolename Like '%市征收%' Or Rolename Like '%超级管理员%') ";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                if (userid != "6506D49F48F9489F9FE921501CF767B0")
                {
                    Button btnSave = (Button)FindControl("btnSave");
                    btnSave.Enabled = false;
                }
                
            }

        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btnInsert = (Button)FindControl("btnInsert");
                        btnInsert.Visible = false;
                    }
                }
            }


        }



        protected void ckb是否已故_CheckedChanged(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>isdisplay("+ckb是否已故.ClientID+")</script>");
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            //if (!ckb是否已故.Checked)
            //{
            //    TextBox txt证件号 = (TextBox)FindControl("txt证件号");
            //    string[] txtsplit = txt证件号.Text.Split(',');
            //    if (txtsplit.Length > 1)
            //    {
            //        for (int i = 0; i < txtsplit.Length; i++)
            //        {
            //            if (txtsplit[i].Length != 15)
            //            {
            //                if (txtsplit[i].Length != 18)
            //                {
            //                    Page.ClientScript.RegisterStartupScript(this.GetType(), "t1", "<script>alert('证件号必须是15或18位，请检查！');</script>");
            //                    return;
            //                }
            //            }
            //        }
            //    }
            //    else
            //    {
            //        if (!string.IsNullOrEmpty(txt证件号.Text))
            //        {
            //            if (txt证件号.Text.Length != 15)
            //            {
            //                if (txt证件号.Text.Length != 18)
            //                {
            //                    Page.ClientScript.RegisterStartupScript(this.GetType(), "t1", "<script>alert('证件号必须是15或18位，请检查！');</script>");
            //                    return;
            //                }
            //            }
            //        }
            //    }
            //}
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            Page.ClientScript.RegisterStartupScript(this.GetType(),"s1","<script>alert('新增成功！');window.location.href = window.location.href</script>");
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Button btnSave = (Button)FindControl("btnSave");
            TextBox txt证件号 = (TextBox)FindControl("txt证件号");
            bool isview = _EluZSDataProvider.GetBZSRXY(HttpContext.Current.Request["ID"]);
            if (isview) { btnSave.Visible = false; }

            string sql = "Select 是否退回 from zs_hbhxy where PERSONID = '" + HttpContext.Current.Request["ID"] + "'";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                if (dt.Rows[0]["是否退回"].ToString() == "1")
                {
                    btnSave.Visible = true;
                    btnSave.Enabled = true;
                }
              
            }

            //if (string.IsNullOrEmpty(txt证件号.Text))
            //{
            //    txt证件号.Text = _EluZSDataProvider.GetBZSRZJH();
            //}
        }
        protected void BindData()
        {
            Button btnSave = (Button)FindControl("btnSave");
            Button btnReturn = (Button)FindControl("btnReturn");
            btnUpload = (Button)FindControl("btnUpload");
            btnSave.Click += new EventHandler(btnSave_Click);
            btnReturn.Click += new EventHandler(btnReturn_Click);
            btnUpload.Click += new EventHandler(btnUpload_Click);
            //if (Page.Request["id"] !=null)
            //{
            //    btnUpload.Text = "附件列表";
            //}
           
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            string strScript = string.Empty;
            string pid = HttpContext.Current.Request["ID"].Trim();
            string pid1 = HttpContext.Current.Request["ID"].Trim();
            Button btnUpload = (Button)FindControl("btnUpload");
            if (btnUpload.Text.Equals("附件列表"))
            {
                strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}','{3}','{4}');</script>", "99",
               Persistence1.KeyValue, pid, pid1, "show");
            }
            else 
            {
                strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}','{3}','{4}');</script>", "99",
                   Persistence1.KeyValue, pid, pid1, "");
            }
           
            Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ckb是否已故.Checked)
            {
                DropDownList ddl证件类型 = (DropDownList)FindControl("ddl证件类型");
                if (ddl证件类型.Items[ddl证件类型.SelectedIndex].Text == "身份证")
                {
                    TextBox txt证件号 = (TextBox)FindControl("txt证件号");
                    string[] txtsplit = txt证件号.Text.Split(',');
                    if (txtsplit.Length > 1)
                    {
                        for (int i = 0; i < txtsplit.Length; i++)
                        {
                            if (txtsplit[i].Length != 15)
                            {
                                if (txtsplit[i].Length != 18)
                                {
                                    Page.ClientScript.RegisterStartupScript(this.GetType(), "t1", "<script>alert('证件号必须是15或18位，请检查！');</script>");
                                    return;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (txt证件号.Text.Length != 15)
                        {
                            if (txt证件号.Text.Length != 18)
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "t1", "<script>alert('证件号必须是15或18位，请检查！');</script>");
                                return;
                            }
                        }
                    }
                }
            }
            
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            //HttpContext.Current.Response.Redirect("ZSPG_ZSPersonInfo.aspx?ID=" + Persistence1.KeyValue);
            string url = "ZSPG_ZSPersonInfo.aspx?ID=" + Persistence1.KeyValue;
            base.Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('保存成功');location.href='"+url+"'</script>");
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZSPG_ZSPerson.aspx");
        }
        protected void QSDataDLL()
        {
            DropDownList ddl证件类型 = (DropDownList)FindControl("ddl证件类型");
            
            DropDownList ddl房屋结构 = (DropDownList)FindControl("ddl房屋结构");

            DropDownList txt国有土地使用证证载用途 = (DropDownList)FindControl("txt国有土地使用证证载用途");
            DropDownList txt房屋用途 = (DropDownList)FindControl("txt房屋用途");


            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("证件类型");
            //_params.Insert(0, null);
            ddl证件类型.DataSource = _params;
            ddl证件类型.DataValueField = "keyvalue";
            ddl证件类型.DataTextField = "keyname";
            ddl证件类型.DataBind();
            ddl证件类型.SelectedIndex = 2;
            List<EluZSProvider.Params> _params1 = _EluZSDataProvider.GetParams("房屋产权证证载用途");
            //_params1.Insert(0, null);            
            List<EluZSProvider.Params> _params2 = _EluZSDataProvider.GetParams("结构等级");
            //_params2.Insert(0, null);
            ddl房屋结构.DataSource = _params2;
            ddl房屋结构.DataValueField = "keyvalue";
            ddl房屋结构.DataTextField = "keyname";
            ddl房屋结构.DataBind();

            List<EluZSProvider.Params> _params5 = _EluZSDataProvider.GetParams("国有土地使用证证载用途");
            txt国有土地使用证证载用途.DataSource = _params5;
            txt国有土地使用证证载用途.DataValueField = "keyvalue";
            txt国有土地使用证证载用途.DataTextField = "keyname";
            txt国有土地使用证证载用途.DataBind();
            txt国有土地使用证证载用途.Items.Insert(0, new ListItem("", ""));
            //txt国有土地使用证证载用途.DataBind();
            txt国有土地使用证证载用途.SelectedIndex = 3;
            List<EluZSProvider.Params> _params6 = _EluZSDataProvider.GetParams("国有土地使用证证载用途");
            txt房屋用途.DataSource = _params6;
            txt房屋用途.DataValueField = "keyvalue";
            txt房屋用途.DataTextField = "keyname";
            txt房屋用途.DataBind();
            txt房屋用途.Items.Insert(0, new ListItem("", ""));
            //txt房屋用途.DataBind();
            txt房屋用途.SelectedIndex = 3;

        }
        protected void BindDll() 
        {
            string userid = string.Empty;
            if (Page.Session["UserID"]!=null)
            {
                 userid = Page.Session["UserID"].ToString();
            }
            string sql1 = @"Select Rolename
  From Zs_Role Where Id in (Select Roleid  From Zs_User_In_Role Where Userid = '" + userid + "')";
            DataTable Rname = _EluZSDataProvider.GetTable(sql1);
            string rolename = string.Empty;
            DropDownList ddlPID = (DropDownList)FindControl("ddlPID");
            string sql = string.Empty;
            if (Rname.Rows.Count>0)
            {
                for (int i = 0; i < Rname.Rows.Count; i++)
                {
                    if (!Rname.Rows[i]["Rolename"].ToString().Equals("超级管理员"))
                    {
                        rolename = Rname.Rows[i]["Rolename"].ToString().Substring(0, 2);
                        sql = "select ID,项目名称 from zs_project where 1=1 and 区属 like'%" + rolename + "%' ";
                    }
                    else
                    {
                        sql = "select ID,项目名称 from zs_project where 1=1 ";
                    }
                }
            }
            
            
           
            if (!string.IsNullOrEmpty(项目名称.Text.Trim()))
            {
                sql += " and 项目名称 like '%" + 项目名称.Text.Trim()    + "%'";
            }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                ddlPID.DataSource = dt;
                ddlPID.DataTextField = "项目名称";
                ddlPID.DataValueField = "ID";
                ddlPID.DataBind();
                if (Page.Request["Id"]!=null)
                {
                    string id = Page.Request["Id"].ToString();
                    string sqlid = "Select * from zs_project where id in (Select pid from ZS_ZSPERSON where id = '" + id + "')";
                    DataTable dt1 = _EluZSDataProvider.GetTable(sqlid);
                    if (dt1.Rows.Count > 0)
                    {
                        string nowvalue = dt1.Rows[0]["ID"].ToString();
                        if (ddlPID.Items.FindByValue(nowvalue)!=null)
                        {
                            ddlPID.SelectedValue = nowvalue;
                        }
                    }
                }
               
            }
            else {
                ddlPID.Items.Clear();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            BindDll();
        }
        [Antu.Core.Ajax.AjaxMethod()]
        public static string getRemark()
        {
            EluZSDataProvider _EluZSDataProvider1 = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
            StringBuilder sbr = new StringBuilder();
            string sql = "select ID,项目名称 from zs_project where 1=1 ";
            DataTable dt = _EluZSDataProvider1.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (sbr.Length > 0) { sbr.Append(","); }
                    sbr.Append(dt.Rows[i]["ID"].ToString() + "||" + dt.Rows[i]["项目名称"].ToString());
                }
            }
            return sbr.ToString();
        }
    }
}


